/**
 * 
 */
package com.jmail.model;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.skn.constant.Constant;

/**
 * @author huanpham
 *
 */
public class MailSender {
	//Properties
	private String strFrom = "";
	private String strUsername = "";
	private String strPassword = "";
	private String strTo = "";
	private String strCC = "";
	private String strBCC = "";
	private String strHost = "";
	private String strSubject = "";
	private String strContent = "";
	
	public MailSender() {
		strFrom = "";
		strUsername = "";
		strPassword = "";
		strTo = "";
		strCC = "";
		strBCC = "";
		strHost = "";
		strSubject = "";
		strContent = "";
	}
	
	public MailSender(String from, String username, String password, String to, String host) {
		strFrom = from;
		strUsername = username;
		strPassword = password;
		strTo = to;
		strHost = host;
	}
	
	public MailSender(String from, String username, String password, String to, String host, String subject) {
		strFrom = from;
		strUsername = username;
		strPassword = password;
		strTo = to;
		strHost = host;
		strSubject = subject;
	}
	
	public MailSender(String from, String username, String password, String to, String host, String subject, String content) {
		strFrom = from;
		strUsername = username;
		strPassword = password;
		strTo = to;
		strHost = host;
		strSubject = subject;
		strContent = content;
	}

	public boolean send() throws AddressException, MessagingException {
		boolean result = true;
		
		Properties props = System.getProperties();
//        props.put("mail.transport.protocol", Constant.PROTOCOL_SMTPS);
//        props.put("mail.smtp.host", Constant.PROTOCOL_SMTP + "." + this.strHost);
        props.put("mail.smtp.auth", "true");

//		Authenticator auth = new Authenticator() {
//			@Override
//			protected PasswordAuthentication getPasswordAuthentication() {
//				return new PasswordAuthentication(strUsername, strPassword);
//			}
//		};
		
        System.out.println("From: " + this.strFrom);
        System.out.println("To: " + this.strTo);
        System.out.println("Host: " + this.strHost);
        
		Session session = Session.getInstance(props, null);
        
        // construct the message
        Message msg = new MimeMessage(session);
        
        if (this.strFrom != null) {
            msg.setFrom(new InternetAddress(this.strFrom));
        } else {
            return false;
        }
        
        msg.setSubject(this.strSubject);
        msg.setText(this.strContent);
        msg.setSentDate(new Date());

        // Using SMTPTransport to send email with authentication 
        Transport transport = session.getTransport("smtps");
        transport.connect(Constant.PROTOCOL_SMTP + "." + this.strHost, this.strUsername, this.strPassword);
        System.out.println("Connect successfully!");
        
        // Using method sendMessage of SMTPTransport 
        transport.sendMessage(msg, InternetAddress.parse(this.strTo, false));
        transport.close();
		
		return result;
	}
	
	public String getStrFrom() {
		return strFrom;
	}

	public void setStrFrom(String strFrom) {
		this.strFrom = strFrom;
	}

	public String getStrUsername() {
		return strUsername;
	}

	public void setStrUsername(String strUsername) {
		this.strUsername = strUsername;
	}

	public String getStrPassword() {
		return strPassword;
	}

	public void setStrPassword(String strPassword) {
		this.strPassword = strPassword;
	}

	public String getStrTo() {
		return strTo;
	}

	public void setStrTo(String strTo) {
		this.strTo = strTo;
	}

	public String getStrCC() {
		return strCC;
	}

	public void setStrCC(String strCC) {
		this.strCC = strCC;
	}

	public String getStrBCC() {
		return strBCC;
	}

	public void setStrBCC(String strBCC) {
		this.strBCC = strBCC;
	}

	public String getStrHost() {
		return strHost;
	}

	public void setStrHost(String strHost) {
		this.strHost = strHost;
	}

	public String getStrSubject() {
		return strSubject;
	}

	public void setStrSubject(String strSubject) {
		this.strSubject = strSubject;
	}

	public String getStrContent() {
		return strContent;
	}

	public void setStrContent(String strContent) {
		this.strContent = strContent;
	}

}
